如何将App装载到模拟器

笔者这里使用 nice 这款App 演示。

step 1

首先使用Mac iTunes下载 对应App的ipa包,再解压,在Payload目录下 我们发现nice这款app的真身: KKShopping.app(难道nice也有一颗电商的心吗?)

使用 xcrun simctl list 查看现有的设备列表。

xcrun-simctl-list

step 2

创建新的模拟器,笔者这里创建iPhone 6搭载iOS9.3系统. 命名为MyiPhone6

1
xcrun simctl create "MyiPhone6" "iPhone 6" "com.apple.CoreSimulator.SimRuntime.iOS-9-3"

simctl-create

step 3

唤起MyiPhone6, 使用Xcode Simulator.app 唤起,参数CurrentDeviceUDID选择我们在xcrun simctl list Devices下看到的MyiPhone6的udid。 注意 一定要唤起,模拟器在shutdown state下是装载不了App的。

1
open -n /Applications/Xcode.app/Contents/Developer/Applications/Simulator.app --args -CurrentDeviceUDID '2C630172-AF2B-4798-B854-3FEE5F221411'

xcrun-simctl-list1

step 4

装载 KKShopping.app

1
xcrun simctl install MyiPhone6 ~/Music/iTunes/iTunes\ Media/Mobile\ Applications/nice\ 4.0.0/Payload/KKShopping.app

install-nice-app

如果,你按照步骤做完之后。 点击模拟器中的nice图标,发现它闪退了。

这里有一个原因是 我们通过iTunes下载下来的包要求运行环境是手机。 而手机和电脑模拟器的CPU架构是不一致的,所以运行不了很正常。

如果,你有源码编译出的Release-iphonesimulator目录下的app文件。 可以试一下下面的命令,在模拟器中唤起你的app

step 5

唤起模拟器中的App

1
xcrun simctl launch booted your.app.bundle_Identifier

唤起时,可以额外设置参数进去
xcrun simctl launch booted your.app.bundle_Identifier -port 7001

对应iOS中的读取逻辑代码为:

1
2
3
4
5
6
7
8
[[[NSUserDefaults standardUserDefaults] dictionaryRepresentation] enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
if([key isEqualToString:@"port"]){
port = obj;
*stop = YES;
}
}];

// port = 7001